﻿<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="System.aspx.cs"
    Inherits="UCenter.Web.Views.Message.System" %>

<asp:content id="Content2" contentplaceholderid="head" runat="server">
 <script type="text/javascript" src="/Content/jquery.json-1.2.js"></script>
 <script type="text/javascript" src="/Content/ui.js"></script>
 <script type="text/javascript" src="/Content/safeauth.js"></script>
 <script type="text/javascript">
 var msgList,throughContainer,throughA,currentUserID,currentFriendID,currrentMsgID;
 $(document).ready(function(){
     msgList = $('#msgList');  
     throughContainer = $('#throughContainer');
     getSystemMessage();
     getGroups(); 
     $('#throughFrm').submit(function(){
          return false;
     });
     $('#ThroughBtn').bind('click',doThrough);
     jQuery().ajaxStart(function(){
          $('#loadImg').show();
     });   
     jQuery().ajaxComplete(function(){
          $('#loadImg').hide();
     }); 
 });
 function getSystemMessage(){
     $('#loadImg').show();    
     $.ajax({
         type:"GET",
         url:"GetInBoxMessage",
         data:"",   
         success:function(res){  
             msgList.html('');  
             var data = eval(res);             
             $.each(data,function(i){
                 var c = data[i];   
                 var sendDate = getDate(c.SentDate);
                 var sp ="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                 var html ="<li class='item'><table width='98%;'><tr><td style='padding:15px;' width='100px' rowspan='2'><img src='/Themes/Standard/Images/sys.gif'/></td><td valign='top' style='padding-top:5px;text-align:left;'><div style='float:left;text-align:left;'>"+c.Content+"</div><div style='float:right;'>"+sendDate+sp+"<a href='javascript:delMsg("+c.ID+");'><img src='/Themes/Standard/Images/del1.gif' align='middle'/></a></div></td></tr>";   
                 if(c.Type==1){
                     if(c.IsNew){
                         html+="<tr><td colspan='2'><div style='padding-top:20px;'><a id='throughA'  href='javascript:through("+c.ID+","+c.FromUser.ID+","+c.ToUser.ID+");'>同意</a></div></td></tr></table></li>";
                     }    
                     else if{
                         html+="<tr><td valign='top'><div style='margin:5px;float:right;' class='modalMessage'>你已同意对方的好友请求，并将对方加为你的好友</div></td></tr></table></li>";
                     }     
                 }   
                 else if(c.Type==2){
                     if(c.Status==0){
                         html+="</table></li>";
                     } 
                 }         
                 msgList.append(html);                
             });             
             $('#loadImg').hide();
         },         
         error:function(){
            alert('get message error!');
            $('#loadImg').hide();
            return;
         }
     });
 }
 
 function through(msgID,userID,friendID){
     currrentMsgID = msgID;
     currentUserID = userID;
     currentFriendID = friendID;
     throughContainer.show();
     throughContainer.dialog({width:420,height:350,overlay: { opacity: 0.5, background: "black" },modal:true});
 }
 
 function doThrough(){
     var groupID =  $("#groupSelector  input[@checked]").val();   
     if(!groupID)
     {
        alert("必须选择一个好友分组！");
        return;
     } 
     var postData ="msgID="+currrentMsgID+"&groupID="+groupID+"&userID="+currentUserID+"&friendID="+currentFriendID+"&note="+escape($("#note").val()); 
     $('#loadImg').show();
     $.ajax({
         type:"POST",
         url:"Through",
         data:postData,   
         success:function(res){ 
             alert("已经通过好友请求，并加对方为好友了！");
             throughContainer.hide();
             throughContainer.dialog('close');             
             $('#loadImg').hide();
             getSystemMessage();
         },         
         error:function(){
            alert('get message error!');
            $('#loadImg').hide();
            return false;
         }
     }); 
     return false;
 }
  
 function getGroups(){         
     $.ajax({
         type:'GET',
         url:'/Friend/GetGroups',
         dataType:'json',
         success:function(res){  
             var data = res;                          
             var column = 3;
             var html="<tr>";             
             $.each(data,function(i){  
                 if((i%column)==0&&i>0)
                 {                   
                      html+="<tr>";    
                 }        
                 html+="<td style='padding:5px;'><input type='radio' value='"+data[i].ID+"' id='selectGroupBox' name='selectGroupBox'></input>"+data[i].GroupName+"</td>";
                 if(i==data.length-1){
                     var k =data.length%column;                    
                     for(var p=0;p<k;p++){
                         html+="<td style='padding:5px;'></td>";
                     }
                 }
                 if(i%column==(column-1)&&i>0)
                 {  
                    html+="</tr>";
                 }   
             });            
             $("#groupSelector").html(html);             
         },
         error:function(){             
             alert('get group error');                 
         }             
     });   
 }
 
 function delMsg(msgID){
      $.ajax({
         type:'POST',
         url:'Delete',
         data:'msgID='+msgID,        
         success:function(res){  
             getSystemMessage();
         },
         error:function(){             
             alert('delete message error');                 
         }             
     });   
 }
 
 function getDate(jsondate){
      return eval("new " + jsondate.substr(1, jsondate.length - 2)).toLocaleDateString();
 }
 </script>
</asp:content>
<asp:content id="Content1" contentplaceholderid="MainContent" runat="server">
<h2><img src='/Themes/Standard/Images/ico_xtxx.gif' align="middle"/>系统消息</h2>
<br />
<img src='/Content/Images/loading.gif' id='loadImg' style="display:none" /> 
<ul id='msgList' style="margin:5px;">
</ul>
<div id='throughContainer' style="display:none;" title='通过好友请求并加为好友'>
 
  <div id='tip' class='modalMessage'>他是您的：</div>
     <div style="height:100px;overflow:auto;">   
     <table id='groupSelector' style="padding:5px;"></table></div>
     <div>备注:</div>
     <div style="padding:5px;"><%=Html.TextArea("note")%></div>
     <div style="padding:5px;"><input type="button" value = "确定" onclick= "doThrough();"></input><input type="button" id="CacelThroughBtn" value="取消"></input></div>

</div>
</asp:content>
